<template>
	<view>
		<view v-if="is_show">
			<view class="address" @click="getaddress" >
				<view class="addinfo" v-if="choose_address">
					<view class="addinfodd" v-if="choose_address.ad1">
						{{choose_address.ad1}}
						{{choose_address.ad2}}
						{{choose_address.ad3}}
						{{choose_address.detail}}
					</view>
					<view class="addinfodd" v-if="choose_address.length == 0">
						<view class="uinfo" style="padding-top: 20px;">请选择收货地址</view>
					</view>
					<view class="uinfo">
						<view style="margin-right: 20px;">{{choose_address.rec_user_name}}</view>
						<view>{{choose_address.rec_mobile}}</view>
					</view>
				</view>
				<view class="addinfo2" wx:else>
					<view class="addinfodd"></view>
					<view class="uinfo" style="padding-top: 20px;">请完善您的收货地址信息</view>
				</view>
				<view class="riicon" >
					<image src="https://ayh.wissun.net/static/newhome/img/riicon.png" />
				</view>
			</view>
			
			<view class="goodsinfo">
				<view class="goodsname">
					<view>{{datalist.goods_info.name}} x1</view>
				</view>
			</view>
			<block v-if="datalist.goods_info.amount">
				<view class="goods_pirce">
					<view>商品金额：</view>
					<view class="pdright" >{{datalist.goods_info.amount}}元</view>
				</view>
			</block>
		
			<view >
				<block v-if="isclick">
					<view id="zhifu"  class="zhifu">确定</view>
				</block>
				
				<block v-if="!isclick">
					<view id="zhifu" @click="wxpay" class="zhifu">确定</view>
				</block>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				datalist:[],
				openid:"",
				is_show:0,
				user_id:0,
				goods_id:0,
				isclick:0,
				trace_code:"",
				product_code:"",
				choose_address:[],
				again:0,
				id:""
			}
		},
		onLoad(p) {
			var that=this;
			var data={}
			that.id=p.id,
			that.product_code=p.product_code,
			that.trace_code=p.sncode
			this.getdata(data)
		},
	    onHide() {
			this.isclick=0;
		},
		onShow() {
			this.isclick=0;
			var choose_address=uni.getStorageSync('choose_address')
			if(choose_address){
			  this.choose_address=JSON.parse(choose_address)
			}
		},
		methods: {
			getdata(data){
				var that=this;
				var data={};
				data.id=this.id
				data.trace_code=this.trace_code
				that.request('/api/code/duihuanbyone', data,"post").then(ret => {
					that.datalist=ret.info
					that.is_show=1;
				})
			},
			getaddress(){
			    uni.navigateTo({
			      url: '/pages/address/choose',
			    })
			},
			//确认提交
			wxpay(){
				var that = this;
				if(that.isclick > 0){ return;}
				uni.showToast({  
					title: '正在请求...',  
					icon: 'loading',
					mask: true,  
					duration: 10000  
				})
				var openid=uni.getStorageSync('openid')
				this.isclick=1;
				var data={}
				data.user_id=uni.getStorageSync('user_id')
				data.trace_code=this.trace_code
				data.open_id=openid
				data.address_id=this.choose_address.id
				data.product_code=this.product_code
				if(this.choose_address.length == 0){
					uni.hideLoading()
					uni.showToast({ title: '请选择收货地址', icon:'none'})
					setTimeout(function(){ that.getaddress() },1500)
					return
				}
				if(this.choose_address){
					that.request('/api/code/get_order_json_beibi_myhy_again', data,"post").then(ret => {
						that.isclick=0;
						//code 0 积分兑换 1微信支付 2错误提示
						if(ret.info.code == 1){
						  uni.hideLoading()
						  var json=ret.info.data
						  uni.requestPayment({
							timeStamp: json.timeStamp,
							nonceStr: json.nonceStr,
							package: json.package,
							signType:json.signType,
							paySign: json.paySign,
							success (res) { 
								uni.showToast({ title: '订单提交成功',})
								setTimeout(function(){
									uni.redirectTo({
									  url: '/pages/naipin/order'
									})
								},2000)
							}
						  })
						}else if(ret.info.code == 0){
						  uni.hideLoading()
						  uni.showToast({  title: ret.info.msg, })
						  setTimeout(() => {
							uni.navigateBack({ delta: 1 })
						  }, 2000);
						}else if(ret.info.code == 2){
						  if(ret.info.msg == "appid和openid不匹配"){
								that.again_wxpay(data)
						  }else{
							uni.hideLoading()
							uni.showToast({  icon:"none",  title: ret.info.msg })
						  }
						}
					})
					
					
					
					
				}else{
					 uni.hideLoading()
					 uni.showToast({ title: '请选择收货地址',  icon:'none' })
					 setTimeout(function(){
						that.getaddress()
					  },2000)
					 return
				}
			},
		}
	}
</script>

<style>
.address{
  width: calc(100%  - 40px);
  margin: 0 auto;
  height: 60px;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  font-size: 14px;
  padding: 10px;
  margin-top: 10px;
}
.showtips{font-size: 12px;color: #999;padding: 10px;}
.addinfo{width: calc(100% - 40px);line-height: 26px;}
.riicon{width: 20px;padding-top: 26px;}
.riicon image{width: 20px;height: 20px;}
page{background: #eee;}
.uinfo{padding-top: 6px;color: #444141;display:flex;}
.goodsinfo{
  background: #fff;
  border-radius: 10px 10px 0px 0px;
  display: flex;
  width: calc(100% - 20px);
  margin: 0 auto;
  margin-top: 10px;
}
.showpic{
  padding: 20px;
  width: 80px;
  height: 80px;
  border-radius: 25px;
}
.goodsname{
  font-size: 14px;
  margin-top: 15px;
  position: relative;
  padding-left: 20px;
}
.prris{
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
.money{color: red;}
.goods_pirce{
  height: 40px;
  line-height: 40px;
  background: #fff;
  padding-left: 20px;
  display: flex;
  border-bottom: 1px solid #eee;
  justify-content: space-between;
  font-size: 14px;
  width: calc(100% - 40px);
  margin: 0 auto;
}
.goods_pirce span{}
.pdright{padding-right: 10px;}
.zhifu{
  height: 40px;
  width: 80%;
  text-align: center;
  background: #3078b1;
  margin: 0 auto;
  color: #fff;
  border-radius: 10px;
  line-height: 40px;
  margin-top: 100px;
}
</style>
